package com.stylefeng.guns.modular.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitGoodsID;
import com.stylefeng.guns.core.util.InitID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.GoodsMapper;
import com.stylefeng.guns.modular.system.model.Consignee;
import com.stylefeng.guns.modular.system.model.Consigner;
import com.stylefeng.guns.modular.system.model.Goods;
import com.stylefeng.guns.modular.system.model.Warehouse;
import com.stylefeng.guns.modular.system.service.IGoodsService;
import com.stylefeng.guns.modular.warehouse.service.IWarehouseService;

/**
 * 货物资料控制器
 *
 * @author fengshuonan
 * @Date 2018-04-04 18:57:49
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    private String PREFIX = "/system/goods/";

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IWarehouseService warehouseService;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 跳转到货物资料首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "goods.html";
    }

    /**
     * 跳转到添加货物资料
     */
    @RequestMapping("/goods_add")
    public String goodsAdd(Model model) {
    	List<Warehouse> warehouses = warehouseService.selectList(null);
    	model.addAttribute("warehouses", warehouses);
        return PREFIX + "goods_add.html";
    }

    /**
     * 跳转到修改货物资料
     */
    @RequestMapping("/goods_update/{goodsId}")
    public String goodsUpdate(@PathVariable Integer goodsId, Model model) {
        Goods goods = goodsService.selectById(goodsId);
        List<Warehouse> warehouses = warehouseService.selectList(null);
    	model.addAttribute("warehouses", warehouses);
        model.addAttribute("item",goods);
        LogObjectHolder.me().set(goods);
        return PREFIX + "goods_edit.html";
    }

    /**
     * 获取货物资料列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Goods> page = new PageFactory<Goods>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(goodsMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Goods> entityWrapper = new EntityWrapper<>();
    		Wrapper<Goods> wrapper = entityWrapper.like("goods_name", condition);
    		page.setRecords(goodsMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }
    
    /**
     * 通过name，模糊查询货物信息
     */
    @RequestMapping(value="/listbyname")
    @ResponseBody
    public Object listByName(@RequestParam String goodsName){
    	EntityWrapper<Goods> entityWrapper = new EntityWrapper<>();
    	Wrapper<Goods> wrapper = entityWrapper.like("goods_name", goodsName);
    	return goodsService.selectList(wrapper);
    }

    /**
     * 新增货物资料
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Goods goods) {
    	goods.setGoodsId(InitGoodsID.getInstance().initUnDisorderID());
        goodsService.insert(goods);
        return SUCCESS_TIP;
    }
    
    /**
     * 获取商品编号
     */
    @RequestMapping(value = "/getgoodsid")
    @ResponseBody
    public Object getGoodsID() {
    	InitGoodsID.getInstance().initUnDisorderID();
        return InitGoodsID.getInstance().initUnDisorderID();
    }

    /**
     * 删除货物资料
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer goodsId) {
        goodsService.deleteById(goodsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改货物资料
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Goods goods) {
        goodsService.updateById(goods);
        return SUCCESS_TIP;
    }

    /**
     * 货物资料详情
     */
    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsId") Integer goodsId) {
        return goodsService.selectById(goodsId);
    }
}
